package com.example.demo.service.impl;

import com.example.demo.controller.UserController;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.*;
import com.example.demo.service.UserService;
import com.example.demo.util.UUIDGenerator;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.Session;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> getUser(Integer id) {
        List<User> user = userMapper.getUser(id);
        return user;
    }

    @Override
    public void register(User user) {
        userMapper.register(user);
    }

    @Override
    public User authenticate(String username, String password) {
        User loginUser = userMapper.authenticate(username,password);
        return loginUser;
    }

    @Override
    public List<Department> getAllDepartment() {
        List<Department> allDepartment = userMapper.getAllDepartment();
        return allDepartment;
    }

    @Override
    public List<Doctor> getAllDoctor() {
        List<Doctor> allDoctor = userMapper.getAllDoctor();
        return allDoctor;
    }

    @Override
    public List<Doctor> getDoctorsByDepartmentId(Long departmentId) {
        return userMapper.getDoctorsByDepartmentId(departmentId);
    }

    @Override
    public List<Schedule> getScheduleByDoctorId(Long doctorId) {
        return userMapper.getScheduleByDoctorId(doctorId);
    }


    @Override
    @Transactional
    public void addAppointment(Schedule schedule, HttpSession session) {


        User user = (User) session.getAttribute("user");


        Appointment appointment = new Appointment();


        appointment.setUserId(user.getId());


        appointment.setScheduleId(schedule.getId());


        String appointmentNumber = UUIDGenerator.generateAppointmentNumber();
        appointment.setAppointmentNumber(appointmentNumber);


        appointment.setQueueNumber(schedule.getTotalNumber()-schedule.getAvailableNumber()+1) ;



        Date now = new Date();
        appointment.setAppointmentTime(now);

        LocalDate localDate = schedule.getDate().toInstant()
                .atZone(ZoneId.of("Asia/Shanghai"))
                .toLocalDate();
        LocalTime startTime, endTime;
        if (schedule.getPeriod() == 1) {
            startTime = LocalTime.of(9, 0);
            endTime = LocalTime.of(12, 0);
        } else {
            startTime = LocalTime.of(13, 0);
            endTime = LocalTime.of(17, 0);
        }

        LocalDateTime visitStart = LocalDateTime.of(localDate, startTime);
        appointment.setVisitTime((Date.from(visitStart.atZone(ZoneId.of("Asia/Shanghai")).toInstant())));


        appointment.setStatus(1);
        log.info("appoinment+{};schedule+{}", appointment, schedule);
        userMapper.addAppointment(appointment);
        addWaitingAppointment(appointment, schedule);
    }

    @Override
    public void updateAvailableNumber(Schedule schedule) {
        schedule.setAvailableNumber(schedule.getAvailableNumber()-1);
        userMapper.updateAvailableNumber(schedule);
    }

    @Override
    public Waiting addWaitingAppointment(Appointment appointment, Schedule schedule) {
        log.info("addWaitingAppointment:{}", appointment);
        Waiting waiting = new Waiting();
        waiting.setAppointmentId(appointment.getId());
        waiting.setDoctorId(schedule.getDoctorId());
        waiting.setStatus(1);
        waiting.setUserId(appointment.getUserId());
        userMapper.addWaitingAppointment(waiting);
        return waiting;
    }

    @Override
    public List<Waiting> getWaiting() {
        return userMapper.getWaiting();
    }

    @Override
    public List<AppointmentInfo> getAppointmentDetails(HttpSession  session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return null;
        }
        Long UserId = user.getId();
        return userMapper.getAppointmentDetails(UserId);
    }

    @Override
    public List<WaitingDetail> getAppointmentWaiting(AppointmentInfo appointmentInfo) {
        List<WaitingDetail> appointmentWaiting = userMapper.getAppointmentWaiting(appointmentInfo);
        if(appointmentWaiting != null){
            int i = 0;
            for (WaitingDetail waitingDetail : appointmentWaiting) {
                waitingDetail.setQueueNumber(i+1);
                i++;
            }
        }
        log.info("getAppointmentWaiting:{}", appointmentWaiting);
        return appointmentWaiting;

    }


}
